/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mycompany.controller.account;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.broadleafcommerce.common.exception.ServiceException;
import org.broadleafcommerce.core.web.controller.account.BroadleafSocialRegisterController;
import org.broadleafcommerce.profile.core.domain.Customer;
import org.broadleafcommerce.profile.web.core.form.RegisterCustomerForm;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
 * The controller responsible for registering a customer
 */
@Controller
@RequestMapping("/register")
public class RegisterController extends BroadleafSocialRegisterController {


	private boolean useEmailForLogin = true;

	@Resource(name="CustomizedRegisterValidator")
	CustomizedValidator customizedValidator;  
	/*	@Autowired
	HCCustomer hCCustomer;

	public void sethCCustomer(HCCustomer hCCustomer) {
		this.hCCustomer = hCCustomer;
	}
	 */
	@RequestMapping(method=RequestMethod.GET)
	public String register(HttpServletRequest request, HttpServletResponse response, Model model,
			@ModelAttribute("registrationForm") RegisterCustomerForm registerCustomerForm) {
		return super.register(registerCustomerForm, request, response, model);
	}

	@RequestMapping(method=RequestMethod.POST)
	public String processRegister(HttpServletRequest request, HttpServletResponse response, Model model,
			@ModelAttribute("registrationForm") RegisterCustomerForm registerCustomerForm, BindingResult errors) throws ServiceException {

		if (useEmailForLogin) {
			Customer customer = registerCustomerForm.getCustomer();
			customer.setUsername(customer.getEmailAddress());
		}

		//RegisterCustomerValidator
		customizedValidator.validate(registerCustomerForm, errors, useEmailForLogin);
		if (!errors.hasErrors()) {
			Customer newCustomer = customerService.registerCustomer(registerCustomerForm.getCustomer(), 
					registerCustomerForm.getPassword(), registerCustomerForm.getPasswordConfirm());
			assert(newCustomer != null);

			// The next line needs to use the customer from the input form and not the customer returned after registration
			// so that we still have the unencoded password for use by the authentication mechanism.
			Authentication auth = loginService.loginCustomer(registerCustomerForm.getCustomer());
			mergeCartProcessor.execute(request, response, auth);            

			String redirectUrl = registerCustomerForm.getRedirectUrl();
			if (StringUtils.isNotBlank(redirectUrl) && redirectUrl.contains(":")) {
				redirectUrl = null;
			}
			return StringUtils.isBlank(redirectUrl) ? getRegisterSuccessView() : "redirect:" + redirectUrl;
		} else {
			return getRegisterView();
		}

		//	return super.processRegister(registerCustomerForm, errors, request, response, model);
	}

	@ModelAttribute("registrationForm") 
	public RegisterCustomerForm initCustomerRegistrationForm(){

		RegisterCustomerForm superForm= super.initCustomerRegistrationForm();
		return superForm;
	}
}

